package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.google.zxing.WriterException;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.home.RechargeActivity;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.CommonUtils;
import com.wbdgj.utils.QRCodeUtil;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

public class WaitPayActivity extends BaseActivity{
    private Context context = this;
    private String result;

    @BindView(R.id.money01)
    TextView money01;
    @BindView(R.id.money02)
    TextView money02;
    @BindView(R.id.statusStr)
    TextView statusStr;
    @BindView(R.id.branchName)
    TextView branchName;
    @BindView(R.id.typeStr)
    TextView typeStr;
    @BindView(R.id.statusStr01)
    TextView statusStr01;
    @BindView(R.id.creat_time)
    TextView creat_time;
    @BindView(R.id.orderId)
    TextView orderId;
    @BindView(R.id.USER_AMOUNT)
    TextView USER_AMOUNT;

    private BigDecimal moneyUser, amount;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_wait_pay;
    }

    @Override
    protected void initView() {
        result = getIntent().getStringExtra("result");
        orderId.setText(result);
        appmborderorderinfo();
    }

    @OnClick(value = {R.id.USER_AMOUNT, R.id.surePay})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.surePay:
                appmborderconform();
                break;
            case R.id.USER_AMOUNT:
                startActivity(new Intent(context, RechargeActivity.class));
                break;
        }
    }

    private void appmborderconform() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appmborderconform(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN),"" );
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    setResult(-1);
                    finish();
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void appmborderorderinfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appmborderorderinfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN),result );
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if (linkedTreeMap.containsKey("ORDER_TYPE")){
                        typeStr.setText(CommonUtils.typeStr(decimalFormat.format(linkedTreeMap.get("ORDER_TYPE"))));
                    }

                    if (linkedTreeMap.containsKey("USER_AMOUNT")){
                        USER_AMOUNT.setText(decimalFormat.format(linkedTreeMap.get("USER_AMOUNT")));
                    }

                    if (linkedTreeMap.containsKey("AMOUNT")){
                        money01.setText(decimalFormat.format(linkedTreeMap.get("AMOUNT")));
                        money02.setText(decimalFormat.format(linkedTreeMap.get("AMOUNT")));
                    }

                    if (linkedTreeMap.containsKey("STATUS")){
                        statusStr.setText(CommonUtils.statusStr(decimalFormat.format(linkedTreeMap.get("STATUS"))));
                        statusStr01.setText(CommonUtils.statusStr(decimalFormat.format(linkedTreeMap.get("STATUS"))));
                    }

                    if (linkedTreeMap.containsKey("CREATE_TIME")){
                        creat_time.setText(CommonUtils.formatDate1(decimalFormat.format(linkedTreeMap.get("CREATE_TIME"))));
                    }

                    if (linkedTreeMap.containsKey("CREATE_TIME")){
                        creat_time.setText(CommonUtils.formatDate1(decimalFormat.format(linkedTreeMap.get("CREATE_TIME"))));
                    }


                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

}
